<template>
  <div>
    <div style="margin-bottom:5px">
      <el-input
          v-model="name"
          placeholder="请输入商品"
          style="width: 200px;"
          prefix-icon="el-icon-search"
          @keyup.enter.native="loadPost">
      </el-input>
      <el-button size="medium" type="primary" @click="loadPost" style="margin-left: 5px">查询</el-button>
      <el-button size="medium" type="success" @click="CleanName" style="margin-left: 5px">重置</el-button>
      <el-button size="medium" type="primary" @click="add" style="margin-left: 5px">新增</el-button>
      <el-button size="medium" type="primary" @click="inGoods" style="margin-left: 5px">入库</el-button>
    </div>

    <el-table :data="tableData"
              :header-cell-style="{background: 'rgba(103,146,146,0.38)'}"
              border
              highlight-current-row
              @current-change="selectCurrentChange">
      <el-table-column prop="id" label="ID" width="140"></el-table-column>
      <el-table-column prop="name" label="商品" width="120"></el-table-column>
      <el-table-column prop="storageId" label="仓库" width="120"  :formatter="formatStorage"></el-table-column>
      <el-table-column prop="goodsTypeId" label="分类" width="120" :formatter="formatGoodsType"></el-table-column>
      <el-table-column prop="count" label="数量" width="120"></el-table-column>
      <el-table-column prop="remark" label="备注"></el-table-column>
      <el-table-column prop="operate" label="操作">
        <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row)">编辑</el-button>
          <el-popconfirm
              title="这是一段内容确定删除吗？"
              @confirm="del(scope.row.id)"
          >
            <el-button slot="reference" size="small" type="danger">删除</el-button>
          </el-popconfirm>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%">
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="商品" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="仓库" prop="storageId">
<!--          <el-input v-model="form.storageId"></el-input>-->
          <el-select v-model="form.storageId" placeholder="仓库">
            <el-option
                v-for="item in storageData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="分类" prop="goodsTypeId">
<!--          <el-input v-model="form.goodsTypeId"></el-input>-->
          <el-select v-model="form.goodsTypeId" placeholder="仓库">
            <el-option
                v-for="item in goodsTypeData"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="phone">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
        title="提示"
        :visible.sync="inDialogVisible"
        width="30%">
      <el-form ref="form1" :rules="rules1" :model="form1" label-width="80px">
        <el-form-item label="商品">
          <el-input v-model="form1.goodsname" readonly></el-input>
        </el-form-item>
        <el-form-item label="数量" prop="count">
          <el-input v-model="form.count"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="phone">
          <el-input v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="inDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "goodsManage",
  data() {
    return {
      currentRow:'',
      tableData: [],
      storageData: [],
      goodsTypeData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      name: "",
      dialogVisible: false,
      inDialogVisible: false,
      form: {
        id: '',
        name: '',
        storageId: '',
        goodsTypeId: '',
        count: '',
        remark: '',
      },
      form1: {
        goodsId: '',
        goodsname: '',
        count: '',
        userId: '',
        adminId: '',
        remark: '',
      },
      rules1: {},
      rules: {
        name: [{ required: true, message: "请输入商品", trigger: "blur" }],}
    };
  },
  methods: {
    selectCurrentChange(val){
this.currentRow = val;
    },
    formatGoodsType(row){
      let temp = this.goodsTypeData.find(item=>{
        return item.id == row.goodsTypeId
      })
      return temp && temp.name
    },
    formatStorage(row){
      let temp = this.storageData.find(item=>{
        return item.id == row.storageId
      })
      return temp && temp.name
    },
    resetForm() {
      this.$refs.form.resetFields();
    },
    resetInForm() {
      this.$refs.form1.resetFields();
    },
    doSave() {
      this.$axios
          .post(this.$httpUrl + "/goods/save", this.form)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success("操作成功！");
              this.dialogVisible = false;
              this.loadPost();
            } else {
              this.$message.error("操作失败！");
            }
          });
    },
    doMod() {
      this.$axios
          .post(this.$httpUrl + "/goods/update", this.form)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success("操作成功！");
              this.dialogVisible = false;
              this.loadPost();
            } else {
              this.$message.error("操作失败！");
            }
          });
    },
    save() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.doMod();
          } else {
            this.doSave();
          }
        } else {
          this.$message.error("请检查输入信息！");
        }
      });
    },
    mod(row) {
      this.form.id = row.id ? Number(row.id) : null;
      this.form.name = row.name;
      this.form.goodsTypeId = row.goodsTypeId;
      this.form.storageId = row.storageId;
      this.form.count = row.count;
      this.form.remark = row.remark;
      this.dialogVisible = true;
    },
    del(id) {
      this.$axios
          .get(this.$httpUrl + "/goods/del?id=" + id)
          .then((res) => {
            if (res.data.code === 200) {
              this.$message.success("删除成功！");
              this.loadPost();
            } else {
              this.$message.error("删除失败！");
            }
          })
          .catch((err) => {
            this.$message.error("请求失败: " + err.message);
          });
    },
    add() {
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.resetForm();
        this.form.id = ''
      });
    },
    inGoods() {
      if(!this.currentRow.id){
        alert("请选择记录");
        return;
      }
      this.inDialogVisible = true;
      this.$nextTick(() => {
        this.resetInForm();
      });
      this.form1.goodsname = this.currentRow.name;
    },
    CleanName() {
      this.name = "";
      this.loadPost();
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.loadPost();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.loadPost();
    },
    loadPost() {
      this.$axios
          .post(this.$httpUrl + "/goods/listPage", {
            pageSize: this.pageSize,
            pageNum: this.pageNum,
            param: {
              name: this.name,
              roleId:'1'
            },
          })
          .then((res) => {
            if (res.data.code === 200) {
              this.tableData = res.data.data;
              this.total = res.data.total;
            } else {
              this.$message.error("获取数据失败");
            }
          });
    },
    loadStorage() {
      this.$axios
          .get(this.$httpUrl + "/storage/list", )
          .then((res) => {
            console.log(JSON.stringify(res))
            if (res.status === 200) {
              this.storageData = res.data;
            } else {
              this.$message.error("获取数据失败");
            }
          });
    },
    loadGoosType() {
      this.$axios
          .get(this.$httpUrl + "/goodsType/list", )
          .then((res) => {
            if (res.status === 200) {
              this.goodsTypeData = res.data;
            } else {
              this.$message.error("获取数据失败");
            }
          });
    },
  },
  beforeMount() {
    this.loadPost();
    this.loadStorage();
    this.loadGoosType();
  },
};
</script>


<style scoped>
</style>
